<?
require_once(dirname(__FILE__).'/../views/helpers/LoginForm.php');
require_once('Zend/Auth.php');

require_once('Helpers/ADODBAuthInterface.php');
require_once('Database/Database.php');

class LoginController extends Main_Controller_Action
{
    public function getForm()
    {
        return new LoginForm(array(
            'action' => '/login/process',
            'method' => 'post',
            'id' => 'loginForm'
        ));
    }

    public function getAuthAdapter(array $params)
    {
       
		try {
			$db = Database::instance();
			//$db->getConn()->debug=1;
		} catch (DatabaseException $e){
			$e->getMessage();
		}
        
        
        //debug($this->_request->getParams());
		$adapter = new ADODBAuthInterface($db);
        
		$adapter->setIdentityColumn('username');
        $adapter->setTableName('users');
        $adapter->setCredentialColumn('password');
        $adapter->setStatusesColumn('statuses_id');
        
        $adapter->setIdentity($this->_request->getParam('username'));
        $adapter->setCredential(trim(md5($this->_request->getParam('password'))));
        $adapter->setStatusesId($this->config->statuses->general->active);
        
        //$adapter->authenticate();
        return $adapter;
        
    }
    
    public function preDispatch()
    {
    	if (Zend_Auth::getInstance()->hasIdentity()) {
            // If the user is logged in, we don't want to show the login form;
            // however, the logout action should still be available
            if ('logout' != $this->getRequest()->getActionName()) {
                $this->_helper->redirector('index', 'index');
            }
        } else {
            // If they aren't, they can't logout, so that action should
            // redirect to the login form
            if ('logout' == $this->getRequest()->getActionName()) {
                $this->_helper->redirector('index');
            }
        }
    }
    
    public function indexAction()
    {
        //$this->view->form = $this->getForm();
        /*$smarty = $this->view->getEngine();
		$tpl = $smarty->fetch('login/index_ajax.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();*/
        $filter = "";
		$orderBy = "ORDER BY id DESC";
		$limit = 30;
		$this->view->buyerRequests = ModelMapper::instance('UsersRequests')->findFilteredPaged($filter, $orderBy, $limit, 0);
    }
    
    public function processAction()
    {
        $request = $this->getRequest();

        // Get our authentication adapter and check credentials
        $adapter = $this->getAuthAdapter(array());
        $auth    = Zend_Auth::getInstance();
        
        $result  = $auth->authenticate($adapter);
        
        if (!$result->isValid()) {
            
            $smarty = $this->view->getEngine();
			$tpl = $smarty->fetch('login/index.tpl');
			
			$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody(json_encode(array('body' => $tpl)));
			$this->getResponse()->sendResponse();
			exit();
            
        } else {
        	echo json_encode(array('redirect' => 1));
        	exit;
        }
		
       //$this->_helper->redirector('index', 'index');
    }
    
    public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
      
		$cart = new Zend_Session_Namespace('cart');
		$cart->cart = 0;
		$checkout = new Zend_Session_Namespace('data');
		$checkout->data = 0;
        $this->_helper->redirector('index', 'index');
    }
}

?>
